#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class Solution {
public:
    int maxTurbulenceSize(vector<int>& arr) {
        
        //滑动数组问题
        int left = 0;
        int right = 1;
        int result = 0;
        int size = arr.size();
        int prestatus = 2;  //0为小于。1为大于
        while(right<size){
            if(arr[right]>arr[right-1]){
                if(prestatus==2){
                    prestatus=1;
                }
                else{
                    if(prestatus==0){
                        prestatus=1;
                    }
                    else{
                        prestatus=1;
                        left=right-1;
                    }
                }
            }
            else if(arr[right]<arr[right-1]){
                if(prestatus==2){
                    prestatus=0;
                }
                else{
                    if(prestatus==1){
                        prestatus=0;
                    }
                    else{
                        prestatus=0;
                        left=right-1;
                    }
                }
            }
            else{
                prestatus=2;
                left=right;
            }
            result = max(result,right-left);
            ++right;
        }
        return result+1;      
    }
};

int main(){
    Solution S;
    vector<int> nums = {2,0,2,4,2,5,0,1,2,3};
    cout<<S.maxTurbulenceSize(nums)<<endl;
}